﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WpfRcp.Core.Views.Presenters;
using Microsoft.Practices.Composite.Presentation.Commands;
using WpfRcp.Core.Services;
using WpfRcp.Modules.Panels.Views.Controllers;
using WpfRcp.Modules.Panels.Properties;
using WpfRcp.Core.Views;

namespace WpfRcp.Modules.Panels.Views.Presenters
{
    public class SecondTabPresentationModel : PresenterBase<ISecondTabView>
    {
        public DelegateCommand<object> SaveFileCommand { get; set; }

        private readonly IContainerFacade m_Container;

        private string m_fileName;
        public string FileName
        {
            get { return m_fileName; }
            set
            {
                m_fileName = value;
                OnPropertyChanged("FileName");
            }
        }

        private string m_firstName;
        public string FirstName
        {
            get { return m_firstName; }
            set
            {
                m_firstName = value;
                OnPropertyChanged("FirstName");
            }
        }

        private string m_lastName;
        public string LastName
        {
            get { return m_lastName; }
            set
            {
                m_lastName = value;
                OnPropertyChanged("LastName");
            }
        }

        public SecondTabPresentationModel(ISecondTabView secondTabView,
            IContainerFacade container)
        {
            View = secondTabView;
            View.Model = this;

            m_Container = container;

            SaveFileCommand = new DelegateCommand<object>(ExecuteSaveFileCommand);
        }

        private void ExecuteSaveFileCommand(object arg)
        {
            var processor = m_Container.Resolve<ContentProcessor>();
            var writer = processor.GetOutput(".", FileName + ".txt");

            if (writer == null)
            {
                var messageBox = m_Container.Resolve<IMessageBox>();
                messageBox.Show(WpfRcp.Modules.Panels.Properties.Resources.Warning, WpfRcp.Modules.Panels.Properties.Resources.FileExists, MessageBoxViewButtons.OK,
                                MessageBoxViewIcons.Warning);
                return;

            }
            writer.WriteLine(WpfRcp.Modules.Panels.Properties.Resources.FirstName + ": " + FirstName);
            writer.WriteLine(WpfRcp.Modules.Panels.Properties.Resources.LastName + ": " + LastName);

            writer.Close();
        }
    }
}
